/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.manager;

import java.util.HashMap;
import java.util.Map;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.util.PreferenceUtils;
import android.content.Context;

import com.google.android.gms.maps.GoogleMap;

/**
 * Stores all application preference default value resolvers
 *
 * @author Justas
 *
 */
public class PreferenceDefaultValueResolvers {

    /**
     * Preferences that have default values should have their {@link DefaultValueResolver}s registered here,
     * so that their default values could be accessed in a simple way.
     */
    private static Map<String, DefaultValueResolver> defaultValueResolvers = new HashMap<String, DefaultValueResolver>();

    static {
        addDefaultValueResolver(StopSearchPreferenceManager.KEY_NEARBY_STOP_RADIUS, new DefaultValueResolver() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T resolveValue(Context context) {
                return (T) (Integer) context.getResources().getInteger(R.integer.pref_stop_search_nearby_stop_radius_default);
            }
        });

        addDefaultValueResolver(PreferenceUtils.SCHEDULE_ACTIVITY_OPENED, new DefaultValueResolver() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T resolveValue(Context context) {
                return (T) Boolean.FALSE;
            }
        });

        addDefaultValueResolver(PreferenceUtils.MAP_TYPE, new DefaultValueResolver() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T resolveValue(Context context) {
                return (T) (Integer) GoogleMap.MAP_TYPE_NORMAL;
            }
        });

        addDefaultValueResolver(PreferenceUtils.SELECTED_SCOPE_FOLDER, new DefaultValueResolver() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T resolveValue(Context context) {
                return (T) context.getResources().getString(R.string.default_scope_folder);
            }
        });

        addDefaultValueResolver(PreferenceUtils.FASTEST_ROUTE_CHANGE_COUNT, new DefaultValueResolver() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T resolveValue(Context context) {
                return (T) (Integer) context.getResources().getInteger(R.integer.default_fastest_route_change_count);
            }
        });

        addDefaultValueResolver(PreferenceUtils.FASTEST_ROUTE_CHANGE_DURATION, new DefaultValueResolver() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T resolveValue(Context context) {
                return (T) (Integer) context.getResources().getInteger(R.integer.default_fastest_route_change_duration);
            }
        });

        addDefaultValueResolver(PreferenceUtils.FASTEST_ROUTE_WALK_DISTANCE, new DefaultValueResolver() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T resolveValue(Context context) {
                return (T) (Integer) context.getResources().getInteger(R.integer.default_fastest_route_walk_distance);
            }
        });

        addDefaultValueResolver(PreferenceUtils.FASTEST_ROUTE_WALK_SPEED, new DefaultValueResolver() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> T resolveValue(Context context) {
                return (T) (Integer) context.getResources().getInteger(R.integer.default_fastest_route_walk_speed);
            }
        });
    }

    /**
     * Add a default value resolver for the preference identified by it's <code>key</code>
     *
     * @param key Preference key
     * @param resolver Default value resolver
     */
    public static void addDefaultValueResolver(String key, DefaultValueResolver resolver) {
        defaultValueResolvers.put(key, resolver);
    }

    /**
     * Resolves default preference value
     *
     * @param context Context
     * @param key Preference key
     * @return Default preference value
     * @see DefaultValueResolver
     */
    public static <T> T resolveDefaultValue(Context context, String key) {
        DefaultValueResolver resolver = defaultValueResolvers.get(key);
        if (null != resolver) {
            return resolver.<T>resolveValue(context);
        }
        else {
            return null;
        }
    }

}
